package com.tim.controller;

import com.tim.entity.User;
import com.tim.entity.UserModel;
import com.tim.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;

	@RequestMapping("/login")
	public String login(User user, HttpServletRequest request) {
		User resultUser = userService.login(user);
		if (resultUser == null) {
			request.setAttribute("user", user);
			request.setAttribute("errorMsg", "用户名密码错误");
			return "/index";
		} else {
			HttpSession session = request.getSession();
			session.setAttribute("currentUser", resultUser);
			return "/success";
		}
	}

    @RequestMapping("/toPostCheckbox")
    public ModelAndView toPostCheckbox ( ) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/user/postCheckbox");
        return  mv;
    }

    @RequestMapping("/testCheck")
    public void testCheck(UserModel userModel,HttpServletRequest req) {
       String userName= req.getParameter("userList[0].userName");
        String id= req.getParameter("userList[0].id");
        System.out.println("userName的值为："+userName);
        System.out.println("id的值为："+id);
            for(User user:userModel.getUserList()){
                System.out.println(user.toString());
            }
        System.out.println("test");

    }

	@RequestMapping("/userList")
	public ModelAndView userList( ) {
		ModelAndView mv = new ModelAndView();
 		List<User> userList = userService.selectAll();
		mv.addObject("userList", userList);
		mv.setViewName("/user/userList");
		return  mv;
	}

	@RequestMapping("/deleteUser/{id}")
	public ModelAndView deleteUser( @PathVariable int id) {
		ModelAndView mv = new ModelAndView("redirect:/user/userList.do");
		userService.deleteByPrimaryKey(id);
		return  mv;
	}

    @RequestMapping(value="/addUser",method = RequestMethod.GET)
    public ModelAndView toAddUser( ) {
        User user=new User();
        ModelAndView mv = new ModelAndView("/user/add");
        mv.addObject("user",user);
        return  mv;
    }

    @RequestMapping(value="/editUser/{id}",method = RequestMethod.GET)
    public ModelAndView toEditUser(@PathVariable int id ) {
        User user=userService.selectByPrimaryKey(id);
        ModelAndView mv = new ModelAndView("/user/edit");
        mv.addObject("user",user);
        return  mv;
    }
    @RequestMapping(value="/addUser",method = RequestMethod.POST)
    public ModelAndView addUser(User user ) {
        ModelAndView mv = new ModelAndView("redirect:/user/userList.do");
        try {
            userService.insertSelective(user);
        }catch (Exception e){
            e.printStackTrace();
        }
        return mv;
    }

    @RequestMapping(value="/editUser",method = RequestMethod.POST)
    public ModelAndView editUser(User user ) {
        ModelAndView mv = new ModelAndView("redirect:/user/userList.do");
            userService.updateByPrimaryKeySelective(user);
        return mv;
    }



}
